/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-23
 * Time: 13:51
 */
//5.最长回文子串
class Solution2 {
    public String longestPalindrome(String s) {
        //1.创建dp表
        int n = s.length();
        //dp[i][j]表示区间[i,j]是否为回文子串
        boolean[][] dp = new boolean[n][n];
        //2.初始化
        //3.状态转移
        int begin = 0,len = 1;//标记起始位置和长度
        for(int i = n-1; i >= 0; i--) {
            for(int j = i; j < n; j++) {
                if(s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = i+1 < j ? dp[i+1][j-1] : true;
                }
                if(dp[i][j] == true && len < j-i+1) {
                    len = j-i+1;
                    begin = i;
                }
            }
        }
        //填表顺序
        //返回结果
        return s.substring(begin,begin+len);
    }
}